package com.mcr.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mcr.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * 自定义元数据对象处理器
 */
@Slf4j
@Configuration
public class MyMetaObjecthandler implements MetaObjectHandler {
    protected Long getId(){
        Long Id;
        if(BaseContext.getUser()!=null){
            Id=BaseContext.getUser();
        }else{
            Id=BaseContext.getEmp();
        }
        return Id;
    }
    @Override//插入操作
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", getId());
        metaObject.setValue("updateUser", getId());
    }

    @Override//修改操作
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", getId());
    }
}
